package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CodeUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.TProd;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ITProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.ValidationException;
import java.util.List;
import java.util.UUID;

/**
 * @author wux
 * @date 2023/5/23 16:26
 */
@Api(tags = "(客户端) 产品接口")
@RestController
@RequestMapping("/client/prod")
public class TProdController {
    @Autowired
    private ITProdService prodService;
    @Autowired
    private ISysDictDataService dictDataService;

    @GetMapping("/list")
    @ApiOperation(value = "产品列表查询")
    public ResultEntity<List<TProd>> list(@ApiParam("项目ID") @RequestParam(value = "projectId") Long projectId,
                                          @ApiParam("产品类型（取自产品类型接口的code）") @RequestParam(value = "prodType", required = false) String prodType) {
        return ResultEntity.createSuccess(prodService.list(new LambdaQueryWrapper<TProd>()
                .eq(TProd::getProjectId, projectId)
                .eq(StringUtils.isNotBlank(prodType), TProd::getProdType, prodType)));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增/编辑产品")
    @Log(title = "新增/编辑产品", businessType = BusinessType.INSERT)
    public ResultEntity saveOrUpdate(@RequestBody TProd params) {
        if (StringUtils.isBlank(params.getProdType())) {
            throw new ValidationException("产品类型不能为空");
        }
        int count = prodService.count(Wrappers.<TProd>lambdaQuery().eq(TProd::getProdType, params.getProdType()));
        // SysDictData tunnel = dictDataService.getByValue("sys_tunnel_area", params.getTunnelArea());
        // SysDictData unit = dictDataService.getByValue("sys_prod_unit", params.getUnit());
        // String prodCode = CodeUtils.getProdCode(params.getTunnel(), tunnel.getDictLabel(), params.getSteel(), unit.getDictLabel());
        // String prodCode = CodeUtils.getProdCode(params.getTunnel(), tunnel.getDictLabel(), params.getSteel(), unit.getDictLabel());
        params.setProdCode(params.getProdType().toUpperCase() + "-" + (count + 1));
        params.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        params.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        params.setCreator(SecurityUtils.getLoginUser().getUser().getName());
        prodService.saveOrUpdate(params);
        return ResultEntity.createSuccess();
    }

    @PostMapping("/del/{id}")
    @ApiOperation(value = "删除产品")
    public ResultEntity delete(@PathVariable("id") Long id) {
        prodService.removeById(id);
        return ResultEntity.createSuccess();
    }

}
